package com.lightsh.widgetdemo;

import java.util.Timer;
import java.util.TimerTask;

import com.lightsh.util.NewsInfo;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.util.Log;
import android.widget.RemoteViews;

public class WidgetDemo extends AppWidgetProvider {
	private static final boolean DEBUG = true;
	private static final String TAG = "com.lightsh.widgetdeom";
	
	public static RemoteViews getFitRemoteView(Context context, NewsInfo newsInfo){
		RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.main);
		
		remoteView.setTextViewText(R.id.NewText, newsInfo.getTitle());
		remoteView.setTextViewText(R.id.PublisherText, newsInfo.getPublisher());
		
		PendingIntent intent = PendingIntent.getActivity(context, 0, new Intent(context, ConfigureActivity.class), 0);
		
		// 设置点击事件
		remoteView.setOnClickPendingIntent(R.id.LeftBtn, intent);
		remoteView.setOnClickPendingIntent(R.id.RightBtn, intent);
		return remoteView;
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		if(DEBUG){
			String logTip = "onUpdate:";
			for(int i = 0; i < appWidgetIds.length; ++i){
				if(i == 0){
					logTip += "[";
				}
				logTip += appWidgetIds[i];
				if(i != appWidgetIds.length - 1){
					logTip += ",";
				}else{
					logTip += "]";
				}
			}
			Log.e(TAG, logTip);
		}
		WidgetService.updateWidgetIds(appWidgetIds);
		
		context.startService(new Intent(context, WidgetService.class).setAction(WidgetService.FLAG_REGET_DATA));
	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		if(DEBUG){
			String logTip = "onDeleted:";
			for (int i = 0; i < appWidgetIds.length; ++i) {
				if (i == 0) {
					logTip += "[";
				}
				logTip += appWidgetIds[i];
				if (i != appWidgetIds.length - 1) {
					logTip += ",";
				} else {
					logTip += "]";
				}
			}
			Log.e(TAG, logTip);
		}
		
		super.onDeleted(context, appWidgetIds);
	}

	@Override
	public void onDisabled(Context context) {
		if(DEBUG){
			Log.e(TAG, "onDisabled");
		}		
		super.onDisabled(context);
	}

	@Override
	public void onEnabled(Context context) {
		if (DEBUG) {
			Log.e(TAG, "onEnabled");
		}
		
		super.onEnabled(context);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		if(DEBUG){
			Log.e(TAG, "onReceive:" + intent.getAction());
		}		
		super.onReceive(context, intent);
	}
	

}    